import sys
import wx
import wx.xrc

from web_request.user_api import req_login


class LoginFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(300, 200), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        login = wx.BoxSizer(wx.VERTICAL)

        login.Add((0, 0), 1, wx.EXPAND, 5)

        username = wx.BoxSizer(wx.HORIZONTAL)

        username.Add((0, 0), 1, wx.EXPAND, 5)

        self.user = wx.StaticText(self, wx.ID_ANY, u"用户名", wx.DefaultPosition, wx.Size(50, -1), 0)
        self.user.Wrap(-1)

        username.Add(self.user, 0, wx.ALL, 5)

        self.username = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(150, -1), 0)
        username.Add(self.username, 0, wx.ALL, 5)

        username.Add((0, 0), 1, wx.EXPAND, 5)

        login.Add(username, 1, wx.EXPAND, 5)

        password = wx.BoxSizer(wx.HORIZONTAL)

        password.Add((0, 0), 1, wx.EXPAND, 5)

        self.word = wx.StaticText(self, wx.ID_ANY, u"密码", wx.DefaultPosition, wx.Size(50, -1), 0)
        self.word.Wrap(-1)

        password.Add(self.word, 0, wx.ALL, 5)

        self.password = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(150, -1), 0)
        password.Add(self.password, 0, wx.ALL, 5)

        password.Add((0, 0), 1, wx.EXPAND, 5)

        login.Add(password, 1, wx.EXPAND, 5)

        button = wx.BoxSizer(wx.HORIZONTAL)

        button.Add((0, 0), 1, wx.EXPAND, 5)

        self.login_button = wx.Button(self, wx.ID_ANY, u"登录", wx.DefaultPosition, wx.DefaultSize, 0)
        self.login_button.Bind(wx.EVT_BUTTON, self.onLogin)  # 绑定登录按钮事件
        button.Add(self.login_button, 0, wx.ALL, 5)

        self.cancel_button = wx.Button(self, wx.ID_ANY, u"取消", wx.DefaultPosition, wx.DefaultSize, 0)
        self.cancel_button.Bind(wx.EVT_BUTTON, self.onCancel)  # 绑定取消按钮事件
        button.Add(self.cancel_button, 0, wx.ALL, 5)

        button.Add((0, 0), 1, wx.EXPAND, 5)

        login.Add(button, 1, wx.EXPAND, 5)  # 移除了 wx.ALIGN_CENTER

        # 添加一个 StaticText 控件用于显示登录结果信息
        self.m_stResultMsg = wx.StaticText(self, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_stResultMsg.Wrap(-1)
        self.m_stResultMsg.Hide()  # 初始状态下隐藏
        login.Add(self.m_stResultMsg, 0, wx.ALL | wx.CENTER, 5)

        login.Add((0, 0), 1, wx.EXPAND, 5)

        self.SetSizer(login)
        self.Layout()

        self.Centre(wx.BOTH)

    def __del__(self):
        pass

    def onConnect(self, event):
        event.Skip()
        sys.exit()

    def onLogin(self, event):
        event.Skip()
        result, token = req_login(self.username.GetValue(), self.password.GetValue())
        if result:
            print("登录成功")
            self.Show(False)
        else:
            self.m_stResultMsg.SetLabel("用户或密码错误")
            self.m_stResultMsg.Show(True)
        print(result)

    def onCancel(self, event):
        event.Skip()
        self.Close()

app = wx.App(False)
frame = LoginFrame(None)
frame.Show(True)
app.MainLoop()
